package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminDishController")
@Slf4j
@RequestMapping("admin/dish")
@Api(tags = "菜品管理")
@RequiredArgsConstructor
public class DishController {
    private final DishService dishService;

    /**
     * 分页查询菜品信息
     *
     * @param pageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> dishPage(DishPageQueryDTO pageQueryDTO) {
        log.info("分页参数：{}", pageQueryDTO);
        // 调用DishService的dishPage方法进行查询
        return Result.success(dishService.dishPage(pageQueryDTO));
    }

    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        // 调用DishService的save方法进行保存
        dishService.save(dishDTO);
        return Result.success("新增成功");
    }

    /**
     * 菜品起售和停售
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售和停售")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "int", paramType = "path"),
                    @ApiImplicitParam(name = "id", value = "菜品ID", required = true, dataType = "long")}
    )
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("菜品起售或停售，状态：{}，菜品ID：{}", status, id);
        // 调用DishService的startOrStop方法进行起售或停售操作
        dishService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 根据id查询菜品信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品信息")
    @ApiImplicitParam(name = "id", value = "菜品ID", required = true, dataType = "long", paramType = "path")
    public Result<DishVO> getDishById(@PathVariable Long id) {
        return Result.success(dishService.getDishById(id));
    }

    /**
     * 更新菜品信息
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "更新菜品信息")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("更新菜品：{}", dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    @ApiImplicitParam(name = "ids", value = "菜品ID列表", required = true, allowMultiple = true, dataType = "long")
    public Result delete(@RequestParam("ids") List<Long> ids) {
        log.info("删除菜品，ID列表：{}", ids);
        dishService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    @ApiImplicitParam(name = "categoryId", value = "分类ID", required = true, dataType = "long")
    public Result<List<Dish>> list(Long categoryId) {
        log.info("根据分类id查询菜品，分类ID：{}", categoryId);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }
}